package com.example.huawei.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.huawei.system.entity.CommunityRelease;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.CommunityReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 社区帖子
 */
@Api(tags = "社区相关接口")
@RestController
@RequestMapping("/community")
public class CommunityReleaseController {

    @Autowired
    private CommunityReleaseService communityReleaseService;

    @ApiOperation(value = "分页查询社区帖子", notes = "分页查询社区帖子")
    @GetMapping("/page")
    public Result<IPage<CommunityRelease>> queryPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     CommunityRelease communityRelease) {
        Page<CommunityRelease> page = new Page<>(pageNo, pageSize);
        return Result.ok(communityReleaseService.queryPage(page, communityRelease));
    }

    @ApiOperation(value = "查询社区帖子详情", notes = "查询社区帖子详情")
    @GetMapping("/info")
    public Result<CommunityRelease> queryPage(CommunityRelease communityRelease) {

        return Result.ok(communityReleaseService.queryById(communityRelease));
    }

    @ApiOperation(value = "新增社区帖子", notes = "新增社区帖子")
    @PostMapping("/add")
    public Result<?> add(@RequestBody CommunityRelease communityRelease) {
        communityRelease.setCreateTime(new Date());
        boolean flag = communityReleaseService.save(communityRelease);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "修改社区帖子", notes = "修改社区帖子")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody CommunityRelease communityRelease) {
        boolean flag = communityReleaseService.updateById(communityRelease);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "删除社区帖子", notes = "删除社区帖子")
    @DeleteMapping("/del/{id}")
    public Result<?> delete(@PathVariable("id") String id) {
        boolean flag = communityReleaseService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }

}
